#!/bin/bash
set -o pipefail;

PGNAME=`basename $0`
PGDIRNAME=`dirname $0`
PGERR="$PGNAME[$$]: "


if [ "${MLPROD_HOME}" == "" ]
then
  echo "${PGERR} :  MLPROD_HOME env not set;  exiting !!!" >&2
  exit 10
fi

PGCFG_FILE="${MLPROD_HOME}/config/mlprog.rc"
if [ ! -f ${PGCFG_FILE} ]
then
  echo "${PGERR} : config file <${PGCFG_FILE}> not found; exiting !!!" >&2
  exit 10
fi

. "${PGCFG_FILE}"


CUST_CTX="$(mlget_cust_ctx 'cust')"
MOD_CTX="$(mlget_cust_ctx  'mod')"
BATCH_WFL_ID=1                  # workflow/product id
BATCH_MODE="oper"
if [ ! -z ${BATCH_RUN_MODE} ]
then
  BATCH_MODE="${BATCH_RUN_MODE}"
fi
LOG_CTX="${BATCH_MODE}-${CUST_CTX}"


PGCFG_FILE="${MLPROD_HOME}/cust/${CUST_CTX}/config/${MOD_CTX}.rc"
if [ ! -f ${PGCFG_FILE} ]
then
  echo "${PGERR} : config file <${PGCFG_FILE}> not found; exiting !!!" >&2
  exit 10
fi

. "${PGCFG_FILE}"
if [ ! -f ${PGCFG_FILE} ]
then
  echo "${PGERR} : config file sourcing <${PGCFG_FILE}> error; exiting !!!" >&2
  exit 10
fi


function cleanup
{
  rm -f ${EDER_DSTATUS_FILE};
  rm -f ${EDER_ESTATUS_FILE};

  if [ -f ${OUT_INSURANCE_FILE} ]
  then
    rm -f ${OUT_INSURANCE_FILE}
    if [ $? -ne 0 ]
    then
      mllogerr "[${LOG_CTX}] : ERR : Unable to cleanup files : ${OUT_INSURANCE_FILE}";
    fi
  fi

}

trap cleanup EXIT ERR TERM INT QUIT


function usage()
{
  mllogerr "Usage: $PGNAME batch_recv_date in MMDDYY_HHMMSS_XXXXXX format [-backup]";
}

if [ $# -lt 1  -o  $# -eq 2  -a  "${2}" != "-backup" ]
then
  usage;
  exit 10;
else
  if [ $# -eq 2 ]
  then
    BACKUP=1;
  else
    BACKUP=0;
  fi
fi

# get the batch dates BATCH_RECV_DATE, BATCH_RUN_DATE, BATCH_EXEC_DATE
mlget_batch_dates "${1}"
if [ $? -ne 0 ] || [ -z "${BATCH_RECV_DATE}" ]
then
  usage
  exit 10
fi

# LOG
LOG_CTX="${LOG_CTX}-${BATCH_RECV_DATE}"


# set file locations
if [ ! -d ${BATCH_CTX_DIR} ]
then
  mkdir ${BATCH_CTX_DIR}
  if [ $? -ne 0 ]
  then
    mllogerr "[${LOG_CTX}] : ERR : unable to create batch dir : ${BATCH_CTX_DIR}"
    exit 10;
  fi
fi

# rcpt for encr
ERCPT_MDLX="-r mkey_mdlx"


#GPG STATUS FILE
EDER_DSTATUS_FILE=${BATCH_CTX_DIR}/${BATCH_RECV_DATE}_ins.d
EDER_ESTATUS_FILE=${BATCH_CTX_DIR}/${BATCH_RECV_DATE}_ins.e
ENCR="${ENCR} -s ${EDER_ESTATUS_FILE}"
DECR="${DECR} -s ${EDER_DSTATUS_FILE}"


# BATCH INSURANCE1 FILE
IN_INSURANCE_FILE=${ARCH_CTX_BAK_DIR}/${BATCH_RECV_DATE}_batch_insurance0_t.gpg
OUT_INSURANCE_FILE=${BATCH_CTX_DIR}/${BATCH_RECV_DATE}_${BATCH_WFL_ID}_batch_insurance0_t.gpg


# EQUIFAX FILE WITH SSN 
IN_EFX_FILE=${ARCH_CTX_IN_DIR}/${BATCH_RECV_DATE}_efx.gpg
OUT_EFX_FILE=${BATCH_CTX_DIR}/${BATCH_RECV_DATE}_${BATCH_WFL_ID}_efx.gpg



if [ ! -r ${IN_INSURANCE_FILE} ]
then
  mllogerr "[${LOG_CTX}] : ERR : batch insurance1 file not found : ${IN_INSURANCE_FILE}";
  exit 10;
fi

if [ -f ${OUT_INSURANCE_FILE} ]
then
#  mllogerr "[${LOG_CTX}] : ERR : Converted batch insurance1 file exist in batch area : ${OUT_INSURANCE_FILE}";
#  exit 10;
  rm -f ${OUT_INSURANCE_FILE}
fi



#---------------------------------
# start loading
#---------------------------------


TASK_ID=0
tstart=$((`date +%s`))

tstamp="00:00:00"
mllogerr "[${LOG_CTX}] : BEG : [${tstamp}] : LOAD DATA PART-1 START"

TASK_ID=`expr $TASK_ID + 1`
tmsg="load batch insurance1 table";
mllogerr "[${LOG_CTX}] : INF : TASK-${TASK_ID} $tmsg";
ts=$((`date +%s`))
${DECR} -f ${IN_INSURANCE_FILE} \
| ${PGSQL} ${SQL_CTX_RUNT_DIR}/load_table_batch_insurance1.sql
if [ $? -ne 0 ] || [ ! -f ${EDER_DSTATUS_FILE} ]
then
  mllogerr "[${LOG_CTX}] : ERR : TASK-${TASK_ID} failed to ${tmsg}";
  cleanup;
  exit 12;
fi
te=$((`date +%s`))


TASK_ID=`expr $TASK_ID + 1`
tmsg="load carrier_code_desc_t table";
mllogerr "[${LOG_CTX}] : INF : TASK-${TASK_ID} $tmsg";
ts=$((`date +%s`))
cat ${SQL_CTX_SETUP_DIR}/dbdata/carrier_code_desc_t.data | ${PGSQL} ${SQL_CTX_SETUP_DIR}/load_table_carrier_code_desc.sql
if [ $? -ne 0 ]
then
  mllogerr "[${LOG_CTX}] : ERR : TASK-${TASK_ID} failed to ${tmsg}";
  cleanup;
  exit -1;
fi
te=$((`date +%s`))


TASK_ID=`expr $TASK_ID + 1`
tmsg="scratching the insurance1_batch_efx_in_t table";
mllogerr "[${LOG_CTX}] : INF : TASK-${TASK_ID} $tmsg";
ts=$((`date +%s`))
${PGSQL} ${SQL_CTX_RUNT_DIR}/delete_efx_table.sql
if [ $? -ne 0 ]
then
  mllogerr "[${LOG_CTX}] : ERR : TASK-${TASK_ID} failed to ${tmsg}";
  cleanup;
  exit -1;
fi
te=$((`date +%s`))


if [ -f ${IN_EFX_FILE} ]
then

	TASK_ID=`expr $TASK_ID + 1`
	tmsg="load Equifax SSN file ";
	mllogerr "[${LOG_CTX}] : INF : TASK-${TASK_ID} $tmsg";
	ts=$((`date +%s`))
	${DECR} -f ${IN_EFX_FILE} \
	| ${PGDIRNAME}/../common/efx_ssn_append.pl  \
	| ${PGSQL} ${SQL_CTX_RUNT_DIR}/load_table_batch_efx_in.sql
	if [ $? -ne 0 ]
	then
  		mllogerr "[${LOG_CTX}] : ERR : TASK-${TASK_ID} failed to ${tmsg}";
  		cleanup;
  		exit -1;
	fi
	te=$((`date +%s`))

fi


TASK_ID=`expr $TASK_ID + 1`
tmsg="load audit funcs";
mllogerr "[${LOG_CTX}] : INF : TASK-${TASK_ID} $tmsg";
ts=$((`date +%s`))
${PGSQL} ${SQL_CTX_RUNT_DIR}/create_funcs_audit.sql
if [ $? -ne 0 ]
then
  mllogerr "[${LOG_CTX}] : ERR : TASK-${TASK_ID} failed to ${tmsg}";
  cleanup;
  exit -1;
fi
te=$((`date +%s`))

TASK_ID=`expr $TASK_ID + 1`
tmsg="audit insurance1 tables";
mllogerr "[${LOG_CTX}] : INF : TASK-${TASK_ID} $tmsg";
ts=$((`date +%s`))
${PGSQL} ${SQL_CTX_RUNT_DIR}/audit_tables_insurance1.sql
if [ $? -ne 0 ]
then
  mllogerr "[${LOG_CTX}] : ERR : TASK-${TASK_ID} failed to ${tmsg}";
  cleanup;
  exit -1;
fi
te=$((`date +%s`))

tstamp="$(mltstamp ${tstart})"
mllogerr "[${LOG_CTX}] : END : [${tstamp}] : LOAD DATA PART-1 DONE"

exit 0;
